"""
@File    : testCase.py
@Author  : qve-wzy
@Time    : 2020/9/2 0002 16:41
"""
import sys
sys.path.append("C:/Users/Administrator/PycharmProjects/appnium-test")
import unittest
import multiprocessing
from util.HTMLTestRunner import HTMLTestRunner
import time
from business.loginBusiness import LoginPageBusiness
from util.server import Server
from  util.wirteUserCommand import WriteUserCommand

class ParameTestCase(unittest.TestCase):
    '''
    创建一个初始化类，用来解决套件传参的问题
    '''
    def __init__(self,methodName='runTest',parame=None):
        super(ParameTestCase, self).__init__(methodName)
        global paramas1
        paramas1 = parame

class CaseTest(ParameTestCase):
    @classmethod
    def setUpClass(cls):
        '''
        配置setupclass
        :return:
        '''
        global paramas1
        print('--setUpClass-->参数',paramas1)
        cls.login_business = LoginPageBusiness(paramas1)

    def setUp(self) -> None:
        print('\n----setUp\n')

    def test_01(self):
        print('----第1次参数 ',paramas1)
        self.login_business.go_login()
        time.sleep(1)
        self.login_business.login_pass()

    def test_02(self):
        print('----第2次参数\n',paramas1)
        self.login_business.go_login()
        flag = self.login_business.login_user_error()
        self.assertTrue(flag)

    def tearDown(self) -> None:
        print('----tearDown')
        time.sleep(1)
        if  sys.exc_info()[0]:
            self.login_business.login_handle.loginPage.driver.save_screenshot("../jpg/test02.png")

    @classmethod
    def tearDownClass(cls) -> None:
        print("tearDownClass")
        time.sleep(1)

def appium_init():
    '''
    启动appium
    :return:
    '''
    server = Server()
    server.main()

def get_suite(i):
    '''
    将测试用例加入suite中，并且运行
    :return:
    '''
    print("get_suite",i)
    suite = unittest.TestSuite()
    suite.addTest(CaseTest("test_01",parame=i))
    suite.addTest(CaseTest("test_02",parame=i))
    # unittest.TextTestRunner().run(suite)
    html_file = "C:/Users/Administrator/PycharmProjects/appnium-test/report/report" + str(i) + ".html"
    fp1 = open(html_file, 'wb')
    # 调用httprunner
    HTMLTestRunner(stream=fp1).run(suite)

def get_count():
    write_user_file = WriteUserCommand()
    num = write_user_file.get_file_lines()
    return num

if __name__ == '__main__':
    appium_init()
    # get_suite(0)
    threads = []
    for i in range(get_count()):
        print("当前i值",i)
        t = multiprocessing.Process(target=get_suite, args=(i,))
        threads.append(t)
    for j in threads:
        j.start()
